package com.example.zbh.dsliao.model;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.zbh.dsliao.model.entity.SearchSinger;
import com.example.zbh.dsliao.model.entity.SearchSong;
import com.example.zbh.dsliao.model.modelinterface.ManagerDataListener;
import com.example.zbh.dsliao.presenter.presenterinterface.HandMessageListener;
import com.example.zbh.dsliao.utils.net.VolleySingleton;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zbh on 15/12/7.
 */
public class SearchModel implements ManagerDataListener {


    @Override
    public void sendResult(String url, final HandMessageListener messageListener) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if(response != null && response.has("data")){
//                            customDialog.dismiss();
                            try {
                                List<SearchSong> songList = null;
                                List<SearchSinger> signerList = null;

                                JSONObject object = response.getJSONObject("data");
                                Gson gson = new Gson();
                                if (object != null && object.has("song")) {
                                    JSONArray array = object.getJSONArray("song");
                                    songList = new ArrayList<SearchSong>();
                                    for (int i = 0; i < array.length(); i++) {
                                        String str = array.getString(i);
                                        SearchSong song = gson.fromJson(str, SearchSong.class);
                                        songList.add(song);

                                    }
                                    messageListener.handResult(songList);
                                }
                                if (object != null && object.has("singer")) {
                                    JSONArray array1 = object.getJSONArray("singer");
                                    signerList = new ArrayList<SearchSinger>();
                                    for (int j = 0; j < array1.length(); j++) {
                                        String str1 = array1.getString(j);
                                        SearchSinger singer = gson.fromJson(str1, SearchSinger.class);
                                        signerList.add(singer);
                                    }
                                }
                                messageListener.handOtherResult(signerList);
//                                adapter.addData(songList, signerList);


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);
    }
}
